package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IAlpAuthorityService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class AlpAuthorityServiceImpl implements IAlpAuthorityService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;

    @Override
    public String authorityDeptFilter(String deptName) {
        SysUser user = ShiroUtils.getSysUser();
        StringBuilder deptNames = new StringBuilder();
        //获取当前用户的 查询权限包含的用户组；
        Set<SysDept> sysDepts = getSearchDeptsByCurrentUser(user);

        if (StringUtils.isNotEmpty(deptName)) {
            //如果指定了 deptName 去用户组查询；
            SysDept searchDept = sysDeptService.selectDeptByName(deptName);
            if (sysDepts.contains(searchDept)) {
                deptNames.append(deptName);
            }
        } else {
            //未指定  直接返回全部；
            for (SysDept u: sysDepts) {
                if (StringUtils.isNotEmpty(deptNames)) {
                    deptNames.append(",");
                }
                deptNames.append(u.getDeptName());
            }
        }
        return deptNames.toString();
    }
    @Override
    public String authorityPersonFilter(String personName) {
        SysUser user = ShiroUtils.getSysUser();
        StringBuilder personNames = new StringBuilder();
        //获取当前用户的 查询权限包含的用户组；
        Set<SysUser> sysUsers = getSearchUsersByCurrentUser(user);

        if (StringUtils.isNotEmpty(personName)) {
            //如果指定了 personName 去用户组查询；
            SysUser searchUser = sysUserService.selectUserByLoginName(personName);
            if (sysUsers.contains(searchUser)) {
                personNames.append(personName);
            }
        } else {
            //未指定  直接返回全部；
            for (SysUser u: sysUsers) {
                if (StringUtils.isNotEmpty(personNames)) {
                    personNames.append(",");
                }
                personNames.append(u.getLoginName());
            }
        }
        return personNames.toString();
    }


    public Set<SysUser> getSearchUsersByCurrentUser(SysUser user) {
        Set<SysUser> result = new HashSet<>();
        List<SysRole> roles = user.getRoles();
        for (SysRole sysRole : roles) {
            //1：所有数据权限；2：自定义数据权限；3：本部门数据权限；4：本部门及以下数据权限
            if ("5".equals(sysRole.getDataScope())) {
                result.add(user);
            }
            if ("4".equals(sysRole.getDataScope()) || "3".equals(sysRole.getDataScope())) {
                SysUser sysUser = new SysUser();
                sysUser.setDeptId(user.getDeptId());
                List<SysUser> users = sysUserService.selectUserList(sysUser);
                result.addAll(users);
            }
            if ("1".equals(sysRole.getDataScope())) {
                SysUser sysUser = new SysUser();
                List<SysUser> users = sysUserService.selectUserList(sysUser);
                result.addAll(users);
            }
        }

        return result;
    }

    public Set<SysDept> getSearchDeptsByCurrentUser(SysUser user) {
        Set<SysDept> result = new HashSet<>();
        List<SysRole> roles = user.getRoles();
        for (SysRole sysRole : roles) {
            //1：所有数据权限；2：自定义数据权限；3：本部门数据权限；4：本部门及以下数据权限
            if ("4".equals(sysRole.getDataScope()) || "3".equals(sysRole.getDataScope())) {
                result.add(user.getDept());
            }
            if ("1".equals(sysRole.getDataScope())) {
                SysDept sysDept = new SysDept();
                List<SysDept> depts = sysDeptService.selectDeptList(sysDept);
                result.addAll(depts);
            }
        }

        return result;
    }
}
